﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 角度标注
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUDimensionRadialRef 的包装</remarks>
    public class DimensionRadial : Dimension
    {
        #region API 函数
        /// <summary>
        /// 创建角度标注
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="path"><see cref="InstancePath"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialCreate(out IntPtr dimension, IntPtr path);

        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="dimension"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialRelease(ref IntPtr dimension);

        /// <summary>
        /// 获取链接曲线
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="path"><see cref="InstancePath"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialGetCurveInstancePath(IntPtr dimension, out IntPtr path);

        /// <summary>
        /// 设置链接到的曲线
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialSetCurveInstancePath(IntPtr dimension, IntPtr path);

        /// <summary>
        /// 是否是直径标注
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="is_diameter"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialGetDiameter(IntPtr dimension, out bool is_diameter);

        /// <summary>
        /// 设置直径标注
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="is_diameter"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialSetDiameter(IntPtr dimension, bool is_diameter);

        /// <summary>
        /// 获取引线断点
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialGetLeaderBreakPoint(IntPtr dimension, ref Point3D point);

        /// <summary>
        /// 设置引线断点
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="point"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialSetLeaderBreakPoint(IntPtr dimension, Point3D point);

        /// <summary>
        /// 获取引线点
        /// </summary>
        /// <param name="dimension"></param>
        /// <param name="points">有三个带你：
        /// <para>[0]：标注文本与引线接触的点</para></param>
        /// <para>[2]：标注箭头与曲线的接触点</para></param>
        /// <para>[1]：标注与圆的接触点</para></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUDimensionRadialGetLeaderPoints(IntPtr dimension, ref Point3D[] points);
        #endregion

        /// <summary>
        /// 引注曲线
        /// </summary>
        public InstancePath CurveInstancePath
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialGetCurveInstancePath(Id, out IntPtr path));
                return new InstancePath(path);
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialSetCurveInstancePath(Id, value.Id));
            }
        }

        /// <summary>
        /// 是否是直径标注
        /// </summary>
        public bool IsDiameterDim
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialGetDiameter(Id, out bool d));
                return d;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialSetDiameter(Id, value));
            }
        }

        /// <summary>
        /// 获取引线断点
        /// </summary>
        public Point3D LeaderBreakPoint
        {
            get
            {
                Point3D p = new Point3D();
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialGetLeaderBreakPoint(Id, ref p));
                return p;
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialSetLeaderBreakPoint(Id, value));
            }
        }

        /// <summary>
        /// 引线点
        /// </summary>
        public List<Point3D> LeaderPoints
        {
            get
            {
                Point3D[] ps = new Point3D[3];
                for (int i = 0; i < 3; i++)
                {
                    ps[i] = new Point3D();
                }
                SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialGetLeaderPoints(Id, ref ps));
                return ps.ToList();
            }
        }

        internal DimensionRadial(IntPtr id) : base(id)
        {
        }

        /// <summary>
        /// 创建一个角度标注
        /// </summary>
        /// <param name="instance"></param>
        /// <returns></returns>
        public DimensionRadial Create(InstancePath instance)
        {
            SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialCreate(out IntPtr id, instance.Id));
            return new DimensionRadial(id);
        }

        public override void Dispose()
        {
            SketchUpExceptionHelper.ThrowSUResult(SUDimensionRadialRelease(ref Id));
        }
    }
}